package com.zha.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

/**
 * @Title: 只有光头才能变强
 * @Author: 炸炸
 * @Date: 2024/12/11 14:58
 * redis配置类
 */
@Configuration
public class RedisCacheConfig {

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
        // 设置redis值超时时间为1天
        redisCacheConfiguration = redisCacheConfiguration.entryTtl(Duration.ofMinutes(60*24));
        // 禁用缓存空值
        redisCacheConfiguration = redisCacheConfiguration.disableCachingNullValues();
        // 设置redis值序列化方式为json
        redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
                RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json())
        );
        return redisCacheConfiguration;
    }
}
